package com.mall.product.controller;

import com.mall.common.domain.MallProductSpell;
import com.mall.common.result.Result;
import com.mall.product.service.ProductInfoService;
import com.mall.product.service.ProductSpellService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wangy
 * @version 1.0
 * @description: TODO
 */

@RestController
public class ProductSpellController {
    @Resource
    private ProductSpellService productSpellService;
    @Resource
    private ProductInfoService productInfoService;

    /**
     * 选择拼单列表
     * * @return Result<List < MallProductSpell>>
     */
    @PostMapping("productSpellList")
    public Result<List<MallProductSpell>> selectProductSpellList(MallProductSpell mallProductSpell) {
        List<MallProductSpell> mallProductSpells = productSpellService.selectProductSpellList(mallProductSpell);
        productSpellService.fail();
        return Result.success(mallProductSpells);
    }

    /**
     * 添加拼单信息
     *
     * @param id 参数
     * @return Result
     */
    @GetMapping("addProductSpell/{id}")
    public Result addProductSpell(@PathVariable Integer id) {
        if (id == null) {
            throw new RuntimeException("参数不能为空");
        }
        MallProductSpell mallProductSpell = new MallProductSpell();
        mallProductSpell.setProductId(id);
        productSpellService.addProductSpell(mallProductSpell);
        productInfoService.updateProduct(id);
        return Result.success();
    }

    /**
     * 更新拼单信息
     *
     * @param mallProductSpell 参数
     * @return Result
     */
    @PostMapping("updateProductSpell")
    public Result updateProductSpell(@RequestBody MallProductSpell mallProductSpell) {
        if (mallProductSpell == null) {
            throw new RuntimeException("参数不能为空");
        }
        productSpellService.updateProductSpell(mallProductSpell);
        productInfoService.updateProduct2(mallProductSpell.getProductId());
        return Result.success();
    }
}
